package com.negativeplus.negativeplusscanner;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

public class MainActivity extends Activity {
	
	private ScanBarcode scannner;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		Button scan = (Button)findViewById(R.id.scan_button);
		Button cancel = (Button)findViewById(R.id.cancelButton);
		final TextView answer = (TextView)findViewById(R.id.answer);
		scan.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				IntentIntegrator integrator = new IntentIntegrator(MainActivity.this);
				integrator.initiateScan();
				answer.setText("");
				answer.setBackgroundColor(Color.TRANSPARENT);
				

			}
		});

		cancel.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				cancelLastScan();
				
			}
		});
	}

	private void sendToServer(CharSequence barcode) {
		// login to barcode scanner interface
		// send value of barcode (id) and choose event by event id
		this.scannner = new ScanBarcode(barcode.toString(), this);
		scannner.execute();
	}


	private void cancelLastScan() {
		if (this.scannner != null){
			this.scannner.cancelLastScan();
		}else {
			DialogManager.showOKDialog(this, false, getString(R.string.cancel_error_title), getString(R.string.cancel_error_message));
		}
		
	}
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		// TODO Auto-generated method stub
		super.onActivityResult(requestCode, resultCode, data);

		if (requestCode==IntentIntegrator.REQUEST_CODE)
		{
			TextView barcode = (TextView)findViewById(R.id.scan_text_field);
			IntentResult scanResult = IntentIntegrator.parseActivityResult(requestCode, resultCode, data);
			if (scanResult != null && scanResult.getContents() != null)
			{
				// handle scan result
				barcode.setText(scanResult.getContents());
				sendToServer(barcode.getText());
			}
			else
			{
				// else continue with any other code you need in the method
				barcode.setText(R.string.scanning_barcode_failed);
			}
		}
	}

	public void setAnswer(final String body, final Boolean isSuccess) {
		
		runOnUiThread(new Runnable() {
			
			@Override
			public void run() {
				TextView answer = (TextView)findViewById(R.id.answer);
				answer.setText(body);
				if(isSuccess){
					answer.setBackgroundColor(Color.GREEN);
					answer.setTextColor(Color.WHITE);
					Button cancel = (Button)findViewById(R.id.cancelButton);
					cancel.setVisibility(View.VISIBLE);
				}
				else{
					answer.setBackgroundColor(Color.RED);
					answer.setTextColor(Color.LTGRAY);
				}
				
			}
		});
		
		
	}

}
